<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义滚动条设计</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            width: 100%; 
            max-width: 800px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }

        header {
            background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
            color: white;
            padding: 25px 30px;
            text-align: center;
        }

        h1 {
            font-size: 2.2rem;
            margin-bottom: 8px;
        }

        .subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .content-wrapper {
            display: flex;
            height: 500px;
        }

        .content {
            flex: 1;
            padding: 25px;
            overflow-y: hidden;
            position: relative;
        }

        .content-text {
            height: 100%;
            overflow-y: auto;
            padding-right: 15px;
        }

        /* 隐藏默认滚动条 */
        .content-text::-webkit-scrollbar {
            width: 0;
            background: transparent;
        }

        .custom-scrollbar {
            width: 12px;
            background: #f1f1f1;
            position: relative;
            border-radius: 0 0 12px 0;
        }

        .scroll-track {
            width: 100%;
            height: 100%;
            background: #f1f1f1;
            border-radius: 10px;
            position: relative;
        }

        .scroll-thumb {
            width: 10px;
            background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);
            border-radius: 10px;
            position: absolute;
            top: 0;
            left: 1px;
            cursor: pointer;
            transition: background 0.3s, width 0.3s;
        }

        .scroll-thumb:hover {
            background: linear-gradient(135deg, #2980b9 0%, #1a252f 100%);
            width: 12px;
            left: 0;
        }

        .scroll-thumb.active {
            background: linear-gradient(135deg, #1a5276 0%, #17202a 100%);
        }

        h2 {
            color: #2c3e50;
            margin: 20px 0 15px;
            padding-bottom: 8px;
            border-bottom: 2px solid #3498db;
        }

        p {
            margin-bottom: 15px;
            text-align: justify;
        }

        .code-block {
            background: #2d2d2d;
            color: #f8f8f2;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Consolas', 'Monaco', monospace;
            overflow-x: auto;
            margin: 15px 0;
            line-height: 1.5;
        }

        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 0 0 12px 12px;
        }

        button {
            background: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s, transform 0.2s;
        }

        button:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }

        .scroll-info {
            text-align: center;
            margin-top: 10px;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <header>
            <h1>自定义滚动条设计</h1>
            <p class="subtitle">使用纯CSS和JavaScript实现</p>
        </header>

        <div class="content-wrapper">
            <div class="content">
                <div class="content-text" id="content">
                    <h2>关于滚动条设计</h2>
                    <p>滚动条是用户界面中常见的组件，用于在有限的空间内浏览超出显示区域的内容。虽然浏览器提供了默认的滚动条样式，但自定义滚动条可以提供更好的用户体验和视觉一致性。</p>

                    <h2>实现原理</h2>
                    <p>自定义滚动条的实现通常涉及以下几个关键步骤：</p>

                    <div class="code-block">
                        // 1. 创建滚动容器和内容区域
                        const content = document.getElementById('content');
                        const scrollThumb = document.getElementById('scrollThumb');
                        const scrollTrack = document.getElementById('scrollTrack');

                        // 2. 计算内容与容器的比例
                        const scrollRatio = content.clientHeight / content.scrollHeight;

                        // 3. 设置滑块高度
                        scrollThumb.style.height = `${scrollRatio * 100}%`;

                        // 4. 添加事件监听器实现交互
                        scrollThumb.addEventListener('mousedown', startDrag);
                    </div>

                    <h2>CSS样式设计</h2>
                    <p>通过CSS，我们可以完全控制滚动条的外观，包括颜色、形状、大小和动画效果。以下是一些常用的CSS属性：</p>

                    <div class="code-block">
                        .scroll-thumb {
                        background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);
                        border-radius: 10px;
                        transition: background 0.3s, width 0.3s;
                        }

                        .scroll-thumb:hover {
                        background: linear-gradient(135deg, #2980b9 0%, #1a252f 100%);
                        width: 12px;
                        }
                    </div>

                    <h2>JavaScript交互</h2>
                    <p>JavaScript负责处理滚动条的交互逻辑，包括：</p>
                    <ul>
                        <li>计算滑块位置和大小</li>
                        <li>处理鼠标拖动事件</li>
                        <li>同步内容区域和滑块的位置</li>
                        <li>添加平滑滚动效果</li>
                    </ul>

                    <h2>响应式设计</h2>
                    <p>现代滚动条设计需要考虑不同设备和屏幕尺寸。通过媒体查询和动态计算，可以确保滚动条在各种环境下都能正常工作。</p>

                    <h2>性能优化</h2>
                    <p>为了提高性能，可以采取以下措施：</p>
                    <ul>
                        <li>使用CSS变换而不是直接修改位置属性</li>
                        <li>限制事件处理频率</li>
                        <li>使用requestAnimationFrame进行平滑动画</li>
                    </ul>

                    <h2>浏览器兼容性</h2>
                    <p>虽然大多数现代浏览器都支持自定义滚动条，但仍需考虑兼容性问题。可以通过特性检测和渐进增强策略确保在各种浏览器中都能提供可用的体验。</p>

                    <h2>可访问性</h2>
                    <p>自定义滚动条不应影响用户的可访问性。确保键盘导航和屏幕阅读器能够正常使用滚动功能。</p>

                    <h2>未来发展趋势</h2>
                    <p>随着CSS Scrollbars规范的发展，未来可能会有更多原生支持自定义滚动条的方法。同时，新的交互模式如触觉反馈和手势控制也将影响滚动条的设计。</p>

                    <h2>设计灵感</h2>
                    <p>优秀的滚动条设计应该与整体界面风格保持一致，同时提供清晰的视觉反馈。可以考虑使用品牌色彩、微妙的动画和适当的阴影效果来提升用户体验。</p>

                    <h2>实现示例</h2>
                    <p>本页面展示了一个完整的自定义滚动条实现，包括轨道、滑块和交互逻辑。您可以通过拖动右侧的滑块或使用下方的控制按钮来体验。</p>
                </div>
            </div>

            <div class="custom-scrollbar">
                <div class="scroll-track" id="scrollTrack">
                    <div class="scroll-thumb" id="scrollThumb"></div>
                </div>
            </div>
        </div>

        <div class="controls">
            <button id="scrollUp">向上滚动</button>
            <button id="scrollDown">向下滚动</button>
            <button id="scrollTop">滚动到顶部</button>
            <button id="scrollBottom">滚动到底部</button>
        </div>

        <div class="scroll-info">
            <p>滚动位置: <span id="scrollPosition">0%</span></p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const content = document.getElementById('content');
            const scrollThumb = document.getElementById('scrollThumb');
            const scrollTrack = document.getElementById('scrollTrack');
            const scrollPosition = document.getElementById('scrollPosition');

            const scrollUpBtn = document.getElementById('scrollUp');
            const scrollDownBtn = document.getElementById('scrollDown');
            const scrollTopBtn = document.getElementById('scrollTop');
            const scrollBottomBtn = document.getElementById('scrollBottom');

            let isDragging = false;

            // 初始化滚动条
            function initScrollbar() {
                const scrollRatio = content.clientHeight / content.scrollHeight;
                scrollThumb.style.height = `${scrollRatio * 100}%`;

                updateScrollThumbPosition();
            }

            // 更新滑块位置
            function updateScrollThumbPosition() {
                const scrollPercentage = content.scrollTop / (content.scrollHeight - content.clientHeight);
                const trackHeight = scrollTrack.clientHeight - scrollThumb.clientHeight;
                scrollThumb.style.top = `${scrollPercentage * trackHeight}px`;

                // 更新滚动位置显示
                scrollPosition.textContent = `${Math.round(scrollPercentage * 100)}%`;
            }

            // 内容滚动事件
            content.addEventListener('scroll', updateScrollThumbPosition);

            // 窗口大小改变时重新初始化
            window.addEventListener('resize', initScrollbar);

            // 滑块拖动功能
            scrollThumb.addEventListener('mousedown', function (e) {
                isDragging = true;
                scrollThumb.classList.add('active');

                const startY = e.clientY;
                const startTop = parseInt(scrollThumb.style.top) || 0;

                function onMouseMove(e) {
                    if (!isDragging) return;

                    const deltaY = e.clientY - startY;
                    const trackHeight = scrollTrack.clientHeight - scrollThumb.clientHeight;
                    let newTop = startTop + deltaY;

                    // 限制滑块在轨道范围内
                    newTop = Math.max(0, Math.min(newTop, trackHeight));

                    scrollThumb.style.top = `${newTop}px`;

                    // 更新内容滚动位置
                    const scrollPercentage = newTop / trackHeight;
                    content.scrollTop = scrollPercentage * (content.scrollHeight - content.clientHeight);
                }

                function onMouseUp() {
                    isDragging = false;
                    scrollThumb.classList.remove('active');
                    document.removeEventListener('mousemove', onMouseMove);
                    document.removeEventListener('mouseup', onMouseUp);
                }

                document.addEventListener('mousemove', onMouseMove);
                document.addEventListener('mouseup', onMouseUp);

                e.preventDefault();
            });

            // 轨道点击功能
            scrollTrack.addEventListener('click', function (e) {
                if (e.target === scrollThumb) return;

                const trackRect = scrollTrack.getBoundingClientRect();
                const clickPosition = e.clientY - trackRect.top;
                const thumbHeight = scrollThumb.clientHeight;

                // 计算新的滑块位置
                let newTop = clickPosition - thumbHeight / 2;
                const trackHeight = scrollTrack.clientHeight - thumbHeight;

                // 限制滑块在轨道范围内
                newTop = Math.max(0, Math.min(newTop, trackHeight));

                scrollThumb.style.top = `${newTop}px`;

                // 更新内容滚动位置
                const scrollPercentage = newTop / trackHeight;
                content.scrollTop = scrollPercentage * (content.scrollHeight - content.clientHeight);
            });

            // 控制按钮功能
            scrollUpBtn.addEventListener('click', function () {
                content.scrollBy({ top: -100, behavior: 'smooth' });
            });

            scrollDownBtn.addEventListener('click', function () {
                content.scrollBy({ top: 100, behavior: 'smooth' });
            });

            scrollTopBtn.addEventListener('click', function () {
                content.scrollTo({ top: 0, behavior: 'smooth' });
            });

            scrollBottomBtn.addEventListener('click', function () {
                content.scrollTo({ top: content.scrollHeight, behavior: 'smooth' });
            });

            // 初始化滚动条
            initScrollbar();
        });
    </script>
</body>

</html>